class QuakeList
{
  String displayedText;
  color theColor;
  int x, y;
  float[] theCoords;
  private PFont quakeFont;
  private int T_WIDTH = 140;
  private int T_HEIGHT = 20;
  private String FILENAME = "eqdata_hi_mag.csv";
  private String DEFAULT_TEXT = "Choose an earthquake";
  private boolean isDropped;
  private boolean isSelected;
  private boolean hasSelection;
  float[][] data;
  
  //constructor
  QuakeList( int r, int g, int b, int x, int y)
  {
    theColor = color( r, g, b);
    this.x = x;
    this.y = y;
    isSelected = false;
    isDropped = false;
    hasSelection = false;
    quakeFont = createFont("Helvetica", 12);
    displayedText = DEFAULT_TEXT;
    theCoords = new float[2];
    getData();
  }
  
  //The draw function for the textfield
  public void render()
  {
    rectMode (CORNER);
    fill(255,255,255);
    text("Largest Earthquakes", x,y,T_WIDTH, T_HEIGHT);
    fill( theColor, 100);
    stroke( theColor, 255);
    strokeWeight(2);
    rect( x, y+T_HEIGHT+10, T_WIDTH, T_HEIGHT);
    fill( theColor, 150);
    stroke( theColor, 255);
    strokeWeight(2);
    rect( x + T_WIDTH, y + T_HEIGHT + 10, T_HEIGHT, T_HEIGHT);
    fill( theColor, 255);
    noStroke();
    beginShape(TRIANGLES);
      vertex( x + T_WIDTH + 4, y + T_HEIGHT + 14);
      vertex( x + T_WIDTH + 16, y + T_HEIGHT + 14);
      vertex( x + T_WIDTH + 10, y + T_HEIGHT + 26);
    endShape();
    fill(255);
    textSize(12);
    textAlign(LEFT, TOP);
    text( displayedText, x + 3, y+T_HEIGHT+10 + 3);
    if( isDropped )
    {
      fill( theColor, 100);
      noStroke();
      rect( x, y + T_HEIGHT*2+10+1, T_WIDTH, ( (16 * 10) + 4) );fill(255);
      textSize(12);
      textAlign(LEFT, TOP);
      for( int i = 0; i < 10; i++)
      {
        int adjustedY = y + T_HEIGHT*2+10 + 3 + (16 * i);
        text( data[i][6] + " - " + int(data[i][1]) + "/" + int(data[i][2]) + "/" + int(data[i][0]), x + 3, adjustedY);
      }
    }
  }
  
  //handles mouse interaction
  public void mouseButtonHandler(int mX, int mY)
  {
    if( (mX > x) && ( mX < ( x + T_WIDTH + 20 ) ) && (mY > y+T_HEIGHT+10) && ( mY < ( y + T_HEIGHT * 2 + 10 ) ) )
    {
      if( isDropped )
      {
        isDropped = false;
      }
      else
      {
        isSelected = true;
        isDropped = true;
      }
    }
    else if( (mX > x) && (mX < x + T_WIDTH)&& (mY>y + T_HEIGHT*2+10)&&(mY<y+250) )//you click on the list
    {
      for( int i = 0; i < 10; i++)
      {
        if( (mY < (y + T_HEIGHT*2+10 + 19 + (16 * i))) && ( mY > (y + T_HEIGHT*2+10+ 3 + (16 * i))) )
        {
          displayedText = data[i][6] + " - " + int(data[i][1]) + "/" + int(data[i][2]) + "/" + int(data[i][0]);
          theCoords[0] = data[i][4];
          theCoords[1] = data[i][5];
          isDropped = false;
          hasSelection = true;
        }
      }
    }
    else
    {
      isSelected = false;
      isDropped = false;
    }
  }
  
  private void getData()
  {
    int rowCount = 0;
    String[] rows = loadStrings(FILENAME);
    data = new float[rows.length][];
    
    for( int i = 0; i < rows.length; i++)
    {
      if(rows[i].length() == 0)
      {
        continue;
      }
      if( rows[i].startsWith("#"))
      {
        continue;
      }
      
      String[] pieces = split(rows[i], ",");
      data[rowCount] = parseFloat( pieces);
      rowCount++;
    }
    data = (float[][]) subset(data, 0, rowCount);
  }
  
  public boolean isSelected()
  {
    return isSelected;
  }
  
  public boolean hasSelection()
  {
    return hasSelection;
  }
  
  public boolean isDropped()
  {
    return isDropped;
  }
  
  public float[] getCoordinates()
  {
    hasSelection = false;
    return theCoords;
  }
  
  public int getX()
  {
    return x;
  }
  
  public int getY()
  {
    return y;
  }
}

